package com.jiukuaidao.zhulin.expand.baidu.webapi;

import com.jiukuaidao.zhulin.api.ApiPoiSearch;
import com.jiukuaidao.zhulin.entity.ApiPoiSearchInfo;
import com.jiukuaidao.zhulin.expand.baidu.ICallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author : BinarySatan
 * Date : 2016/5/25
 * Description : 百度poi检索, 根据信息 搜索出附近位置
 */
public class PoiSearch {
    public static void search(String query, String region, final ICallBack<ApiPoiSearchInfo> callBack) {
        new ApiPoiSearch<ApiPoiSearchInfo>(query, region, new Callback<ApiPoiSearchInfo>() {
            @Override
            public void onResponse(Call<ApiPoiSearchInfo> call, Response<ApiPoiSearchInfo> response) {
                if (HttpURLConnection.HTTP_OK == response.code()) { // 200成功
                    if (0 == response.body().status) {
                        callBack.onSuccess(response.body());
                    } else {
                        callBack.onFailed("state :" + String.valueOf(response.body().status));
                    }
                } else { //非200
                    callBack.onFailed("errorCode :" + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiPoiSearchInfo> call, Throwable t) {
                callBack.onFailed(t.getMessage());
            }
        }).start();
    }
}
